/******************************************************************************
Author: Akshay Dixit
Date Created: September 25, 2018
Date last modified: June 15, 2020
File Name: 3_tz.do
Project: T4D Tanzania 
Purpose: Analysis of secondary outcomes
******************************************************************************/

/* LIST OF SECONDARY OUTCOMES & THE ASSOCIATED VARIABLES


RQ.1 Uptake of health services
	
	1. Birth preparedness - pf48a pf48b pf48c pf48d pf48e pf48f pf48g pf48h 
	
	NOTE: Birth preparedness is slightly different from the description in the
	Pre-Analysis Plan. It has one additional component (pf48g: Prepare materials
	for the delivery). 

RQ.3 Health outcomes

	1. Birth weight - pf12 pf13 pf13_source pf14
	2. Maternal depression - md01 md02 md03 md04 md05 md06
	
*/

clear all
set more off

u "$data/T4D_end_hh_clean.dta", clear

********************************************************************************

* PREPARING SECONDARY OUTCOMES - RQ.1

***Birth preparedness***
desc pf48a pf48b pf48c pf48d pf48e pf48f pf48g pf48h 

			*Check if the skip pattern on pf48f working correctly?
			*pf45F: Identify childcare/who would take care of your other children
			tab sk01 if pf48f == .
			tab sk01a if pf48f == .
			*Yes, it working correctly
			
	//Replace missing values of pf48f as "Yes" - women who did not have/were not responsible for other children
	//are treated as having "done" that bit of the birth preparedness plan.
replace pf48f = 1 if pf48f == .
			
local bp pf48a pf48b pf48c pf48d pf48e pf48f pf48g pf48h 
foreach var of local bp {
	replace `var' = 0 if `var' == 2
	tab `var'
}

egen birth_preparedness = rowtotal(pf48a pf48b pf48c pf48d pf48e pf48f pf48g pf48h), missing
sum birth_preparedness
				
********************************************************************************

* PREPARING SECONDARY OUTCOMES - RQ.3

***Birth weight***
desc pf12 pf13 pf13_source
tab pf12 				//4056 infants weighed at birth
tab pf13				//3952 measurements available
count if pf13 == .d		//104 "Don't Knows"
replace pf13 = . if pf13 == .d

bysort treatment: sum pf13	
bysort treatment: count if pf13 == .	//1021 missing values in treatment
										//1035 missing values in control

g birthweight = pf13
g low_birthweight = (birthweight < 2.5)
replace low_birthweight = . if birthweight == .
g low_birthweight_strict_impute = low_birthweight
g low_birthweight_impute = low_birthweight

bysort pf13_source: tab low_birthweight	//From child card - 4.4% low birthweight
										//Not from child card - 6.3% low birthweight, i.e. slightly overestimated.

tab pf14	//Overall, 12% respondents judged their babies as being "Smaller than average" or "Very small" at birth
			//Of these, 3.76% respondents judged their babies as being "Very small".
			//This is quite close to what we see in the child cards (4.4%)
tab pf14 if low_birthweight == 1 & pf13_source == 1

			/*	Of the card-reported low-birthweight children:
			
					20% (22) women thought their babies were Very Small at birth
					42% (46) thought their babies were Smaller Than Average
					Less than 4% thought their babies were Large/Very Large.
				
			*/
			
tab low_birthweight if pf14 == 5	
tab low_birthweight if pf14 == 4 | pf14 == 5		

			/*	226 women reported having a "Very Small" baby:
					
					Of these, 143 (63%) had card-reported birthweight measurement.
					Of these 143, 45 (31%) were actually low birthweight.
					
				723 women reported having a "Smaller than average" or "Very Small" baby.
				
					Of these, 480 (66%) had card-reported birthweight measurement. 
					Of these 480, 125 (26%) were actually low birthweight.
			
				Overall, it seems like women tend to overestimate low birthweight.
			
			*/
			
	*Imputation 1: Assuming "Very Small" AND "Smaller than average" corresponds to low birthweight
replace low_birthweight_impute = 1 if low_birthweight_impute == . & (pf14 == 4 | pf14 == 5)
replace low_birthweight_impute = 0 if low_birthweight_impute == . & (pf14 == 1 | pf14 == 2 | pf14 == 3)

	*Imputation 2: Assuming only "Very Small" corresponds to low birthweight
replace low_birthweight_strict_impute = 1 if low_birthweight_strict_impute == . & (pf14 == 5)
replace low_birthweight_strict_impute = 0 if low_birthweight_strict_impute == . & (pf14 == 1 | pf14 == 2 | pf14 == 3 | pf14 == 4)

tab low_birthweight					//5.11% low birthweight without any imputation
tab low_birthweight_strict_impute	//4.75% low birthweight assuming "Very Small" corresponds to low birthweight
tab low_birthweight_impute			//7.41% low birthweight assuming "Very Small" and "Smaller than average" corresponds to low birthweight


***Maternal depression***
desc md01 md02 md03 md04 md05 md06

	//Converting this from a 1-5 to a 0-4 scale
lab dir
lab drop md01 md02 md03 md04 md05 md06	//Dropping labels to avoid confusion

local md md01 md02 md03 md04 md05 md06
foreach var of local md {
	replace `var' = . if `var' == .d | `var' == .n | `var' == .r
	replace `var' = `var' - 1 if `var' != .
	tab `var'
}

egen mother_k6_score = rowtotal(md01 md02 md03 md04 md05 md06), missing
replace mother_k6_score = . if md01 == . | md02 == . | md03 == . | md04 == . | md05 == . | md06 == .
tab mother_k6_score	//The higher the K6 score, the lower the level of depression

	/*
	
	The Harvard Med School doesn't provide any specific guidance on how to treat missing values.
	Here, if any of the 6 components is missing, the total K6 score is coded as missing. 
	This approach is conservative, and with this conservative approach we have 149 missing values.
	
	*/

********************************************************************************

* RESULTS 

***Label variables***
lab var birth_preparedness "Birth preparedness"
lab var low_birthweight_strict_impute "Low birthweight"
lab var mother_k6_score "Maternal depression (K6 score)"
lab var treatment "Treatment"

***Produce Excel output***
cd "$analysis"

cap erase "secondary_outcomes.xls"

local outcomes low_birthweight_strict_impute mother_k6_score birth_preparedness 
foreach var of local outcomes {
	sleep 2000
	qui sum `var' if treatment == 0
	local control_mean = (r(mean))
	local rounded = round(`control_mean', 0.01)
	qui reg `var' treatment $strata, vce(cluster facility_id)
	outreg2 using "secondary_outcomes.xls", append label nonote nor2 keep(treatment) ///
	addtext(Control Mean, "`rounded'") ///
	addnote("Notes: Robust standard errors clustered at the facility-level in parentheses.", "All regressions include strata-specific binary variables.", "*** p<0.01, ** p<0.05, * p<0.1")
}

cap erase "secondary_outcomes.txt"

* ADDING BASELINE OUTCOME TO REGRESSION SPECIFICATION

merge m:1 village_id using "$data/baseline_outcomes_by_village.dta"

cap erase "secondary_outcomes_with_baseline.xls"

ren low_birthweight_strict_impute low_birthweight_strict
ren m_bl_low_birthweight m_bl_low_birthweight_strict

local secondary low_birthweight_strict birth_preparedness mother_k6_score
foreach var of local secondary {
	di "`var'"
	reg `var' treatment $strata m_bl_`var', vce(cluster facility_id)
	outreg2 using "secondary_outcomes_with_baseline.xls", append label keep(treatment m_bl_`var')
}

cap erase "secondary_outcomes_with_baseline.txt"

********************************************************************************

clear

